include("Initialize.jl")

using PyCall

function  BoundaryCondition(Q, Op, Geo, t, SimType)
    """
    Purpose: Impose boundary conditions on 2D Euler equations on weak form
    """
    # mapB = [Geo.mapI; Geo.mapO; Geo.mapW]

    # for n=1:4
    #     Qn = Q[:,:,n]
    #     Qbcn = Qbc[:,:,n]
    #     Qn[mapB] = Qbcn[mapB]
    #     Q[:,:,n] = Qn
    # end

    # a python function to re-order boundary ids
    
    py"""
    from collections import OrderedDict
    def sort_map(map_in, x):
        map_str = [str(i) for i in map_in]
            
        sort = OrderedDict()
        for i in range(len(map_in)):
            sort[map_str[i]] = x[i]            
                
        map_in_sorted = OrderedDict(sorted(sort.items(),key=lambda t:t[1]))
        map_in_n = [k for k, v in map_in_sorted.items()]
        return [int(i) for i in map_in_n]
    """
    

    # re-order In, Out, Up and Low map
    mapI = Geo.mapI; xI = Geo.FPs[:,:,2][Geo.mapI]
    mapI = py"sort_map"(mapI, xI)

    mapO = Geo.mapO; xO = Geo.FPs[:,:,2][Geo.mapO]
    mapO = py"sort_map"(mapO, xO) 

    mapU = Geo.mapU; xU = Geo.FPs[:,:,1][Geo.mapU]
    mapU = py"sort_map"(mapU, xU)

    mapL = Geo.mapL; xL = Geo.FPs[:,:,1][Geo.mapL]
    mapL = py"sort_map"(mapL, xL)

    # set periodic boundary conditions
    for n=1:4
        Qn = Q[:,:,n]
        
        # In and out
        tmp = Qn[mapO]; Qn[mapO] = Qn[mapI]; Qn[mapI] = tmp
      
        # Up and Low
        tmp = Qn[mapL]; Qn[mapL] = Qn[mapU]; Qn[mapU] = tmp
        
        Q[:,:,n] = Qn
    end

    Q
end